package com.boliq.lol.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.boliq.lol.R;
import com.boliq.lol.adapter.MenuAdapter;
import com.boliq.lol.base.BaseActivity;
import com.boliq.lol.bean.MenuBean;
import com.boliq.lol.ui.activity.SettingActivity;
import com.boliq.lol.ui.activity.SettingActivity1;
import com.boliq.lol.ui.fragment.UserCenterFragment;
import com.boliq.lol.ui.news.NewsMainFragment;
import com.boliq.lol.ui.view.RoundImageView;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import blq.ssnb.snbutil.SnbToast;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private DrawerLayout mDrawerLayout;

    //左边滑出来的View>>>>>>>>
    private ImageView userHeadView;//用户头像
    private View scanView;//扫描按钮
    private View moreView;//更多按钮
    private TextView userNameView;//用户名名称
    private TextView levelView;//用户等级
    private TextView userSignatureView;//用户签名
    private RecyclerView menuListView;//左边的操作列表
    private MenuAdapter mMenuAdapter;//操作列表对应的adapter
    private View settingBtn;//设置按钮
    //<<<<<<<<<<<<<<<<<<<<<

    //中间内容的布局
    private View rootContentView;
    private View drawerBtn;

    private BottomNavigationView mBottomNavigationView;//底部导航栏的view
    //中间内容的缓存
    private final SparseArray<Fragment> mFragmentCache = new SparseArray<>();
    private RelativeLayout top;
    ImageView nav1;
    ImageView nav2;
    ImageView nav3;
    RoundImageView headbtn;
    TextView name;
    @Override
    protected int contentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        top=findViewById(R.id.top);
        nav1=findViewById(R.id.img_nav_top_one_btn);
        nav2=findViewById(R.id.img_nav_top_two_btn);
        nav3=findViewById(R.id.img_nav_top_three_btn);
        nav1.setOnClickListener(this);
        nav2.setOnClickListener(this);
        nav3.setOnClickListener(this);
        mDrawerLayout = findViewById(R.id.drawer_layout);
        name=findViewById(R.id.tv_user_nick);
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBottomNavigationView.setSelectedItemId(R.id.tab_user_center);
            }
        });

        settingBtn = findViewById(R.id.ll_setting_btn);
        scanView = findViewById(R.id.img_scan_btn);
        userHeadView = findViewById(R.id.img_user_head);
        moreView = findViewById(R.id.img_more_btn);
        userNameView = findViewById(R.id.tv_user_nick);
        levelView = findViewById(R.id.tv_user_level);
        userSignatureView = findViewById(R.id.tv_signature);
        menuListView = findViewById(R.id.rv_menu_list);
        rootContentView = findViewById(R.id.view_main_content);
        drawerBtn = findViewById(R.id.img_drawer_btn);
        mBottomNavigationView = findViewById(R.id.bottom_nav_view);

        headbtn=findViewById(R.id.img_user_head);

    }

    @Override
    protected void initData() {
        menuListView.setLayoutManager(new LinearLayoutManager(getContext()));
        mMenuAdapter = new MenuAdapter(this);
        menuListView.setAdapter(mMenuAdapter);

    }

    @Override
    protected void bindData() {
        super.bindData();
        userNameView.setText("紫米");
        levelView.setText("Lv10");
        userSignatureView.setText("皮肤爱好者  <yyds!!!>");

        List<MenuBean> menuBeans = new ArrayList<>();
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_message_24, "我的评论"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_mail_24, "与我相关"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_assignment_24, "我的订单"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_check_box_24, "我的任务"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_loyalty_24, "我的收藏"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_card_giftcard_24, "活动中心"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_verified_user_24, "信用分"));
        mMenuAdapter.addData(menuBeans);
        userHeadView.setImageResource(R.drawable.icon_tianguan);

        mBottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            if(item.getItemId()==R.id.tab_user_center){

            }
            Fragment fragment = getContent(item);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fl_main_content, fragment)
                    .commit();
            return true;
        });
        mBottomNavigationView.setSelectedItemId(R.id.tab_news);
    }

    private Fragment getContent(MenuItem menuItem) {
        int id = menuItem.getItemId();
        Fragment fragment = mFragmentCache.get(id);
        if (fragment == null) {
            fragment = nf(menuItem);
            mFragmentCache.put(id, fragment);
        }
        return fragment;
    }

    private Fragment nf(MenuItem menuItem) {
        Fragment fragment = null;
        switch (menuItem.getItemId()){
//            case R.id.tab_news:
//                fragment =new NewsMainFragment();
//                break;
            case R.id.tab_user_center:

                fragment = new UserCenterFragment();
                break;

//            case R.id.tab_store:
//                fragment = new StoreFragment();
//                break;
            default:

                fragment =new NewsMainFragment();
        }

        fragment.setArguments(NewsMainFragment.newArgument(menuItem.getTitle().toString()));
        return fragment;
    }

    @Override
    protected void bindEvent() {
        settingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, SettingActivity.class));
            }
        });
        scanView.setOnClickListener(v -> SnbToast.showSmart(getContext(), "点击扫描"));
        headbtn.setOnClickListener(v -> SnbToast.showSmart(getContext(), "资料修改"));
        moreView.setOnClickListener(v -> {
            SnbToast.showSmart(getContext(), "点击更多");
            Toast.makeText(getContext(),"土司内容",Toast.LENGTH_LONG).show();
        });
        drawerBtn.setOnClickListener(v -> mDrawerLayout.openDrawer(GravityCompat.START));

        mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                float diff = drawerView.getWidth() + drawerView.getX();
                rootContentView.setTranslationX(diff);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_nav_top_one_btn:
                Toast.makeText(this,"兴趣",Toast.LENGTH_SHORT).show();
                break;
            case R.id.img_nav_top_two_btn:
                Toast.makeText(this,"云顶",Toast.LENGTH_SHORT).show();

                break;
            case R.id.img_nav_top_three_btn:
                Toast.makeText(this,"进入掌盟",Toast.LENGTH_SHORT).show();

                break;

        }
    }

}